<?php
namespace App\Services\Common\Log;

use Elasticsearch\ClientBuilder;

/**
 * es存储日志记录
 *
 * @author: yanxs <541139655@qq.com>
 * @version: 1.0
 * @date: 2020/12/3
 */
class Elasticsearch implements LogInterface
{

    protected $index; //索引
    protected $client;
    public function __construct()
    {
        //历史原因，默认做异常日志,因为这一块之前只纯粹做异常日志的
        $this->index = config('common.log.default_table');
        //$this->client =
    }

    public function setCollection($collection)
    {
        $this->index = $collection;
    }

    /** 记录日志
     * @param $data
     */
    public function record($data)
    {
        $config =[
            config("common.elasticsearch.host")
        ];

        $client = ClientBuilder::create()->setHosts($config)->build();
        $params = [
            "index" => $this->index,
            "id"    => uniqid(),
            "body"  => $data
        ];
        $client->create($params);
    }
}
